/**
 * 通用转换工具
 */
function sConverter() {
    /**
     * 将padding或margin字符串解析为对象
     * @param {*} pom
     * @returns 
     */
    this.parsePoM = function (pom) {
        var result = {
            top: 0,
            right: 0,
            bottom: 0,
            left: 0
        };

        if (!pom) return result;

        if(pom instanceof Object){
            for(var i in result){
                if(pom[i] !== undefined){
                    result[i] = parseFloat(pom[i]);
                }
            }
            return result;
        }

        pom = pom.toString();

        var arr = null;

        if (pom.indexOf(",") > -1) {
            arr = pom.split(",");
        }else if(pom.indexOf(" ") > -1) {
            arr = pom.split(" ");
        }else{
            arr = [pom];
        }

        if(arr){
            result.top = parseFloat(arr[0]);

            if (arr.length == 1) {
                result.right = result.top;
                result.bottom = result.top;
                result.left = result.top;
            }
            if (arr.length > 1) {
                result.right = parseFloat(arr[1]);
            }
            if (arr.length == 2) {
                result.bottom = parseFloat(arr[0]);
                result.left = parseFloat(arr[1]);
            }
            if (arr.length > 2) {
                result.bottom = parseFloat(arr[2]);
            }
            if (arr.length > 3) {
                result.left = parseFloat(arr[3]);
            }
        }

        return result;
    };

    /**
     * 将padding或margin对象转成字符串
     * @param {*} pom 
     * @returns 
     */
    this.stringifyPoM = function(pom) {
        var str = "";
        
        if(typeof(pom) == "object"){
            if(pom.top){
                str += pom.top + " ";
            }
            if(pom.right){
                str += pom.right + " ";
            }
            if(pom.bottom){
                str += pom.bottom + " ";
            }
            if(pom.left){
                str += pom.left + " ";
            }

            str = str.length > 0 ? str.substring(0, str.length - 1) : str;
        }

        return str;
    }

    /**
     * 像素转数字
     * @param {*} pxs 多个使用数组
     * @returns 
     */
    this.px2Num = function (pxs) {
        if (pxs instanceof Array) {
            var arr = [];
            for (var i = 0; i < pxs.length; i++) {
                var px = pxs[i];
                arr.push(parseFloat(px ? (px + "").replace("px", "") : 0));
            }
            return arr;
        }
        return parseFloat(pxs ? (pxs + "").replace("px", "") : 0);
    }

    /**
     * 增色或褪色
     * @param {*} p 小数，正数表示褪色，负数表示增色 
     * @param {*} from 要改变的颜色
     * @param {*} to 混合色
     * @returns 
     */
    this.lightOrDarkColor = function (p, from, to) {
        var r = Math.round, 
            h = from.length > 9, 
            h = typeof (to) == "string" 
                ? to.length > 9 
                    ? true 
                    : to == "c" 
                        ? !h 
                        : false 
                : h, 
            b = p < 0, 
            p = b 
                ? p * -1 
                : p, 
            to = to && to != "c" 
                ? to 
                : b 
                    ? "#000000" 
                    : "#FFFFFF", 
            f = sbcRip(from), 
            t = sbcRip(to);

        if (h) 
            return "rgb(" + 
                r((t[0] - f[0]) * p + f[0]) + "," + 
                r((t[1] - f[1]) * p + f[1]) + "," + 
                r((t[2] - f[2]) * p + f[2]) + 
                (f[3] < 0 && t[3] < 0 
                    ? ")" 
                    : "," + 
                (f[3] > -1 && t[3] > -1 
                    ? r(((t[3] - f[3]) * p + f[3]) * 10000) / 10000 
                    : t[3] < 0 
                        ? f[3] 
                        : t[3]) + 
                ")"
            );
        else 
            return "#" + 
                (0x100000000 + 
                (f[3] > -1 && t[3] > -1 
                    ? r(((t[3] - f[3]) * p + f[3]) * 255) 
                    : t[3] > -1 
                        ? r(t[3] * 255) 
                        : f[3] > -1 
                            ? r(f[3] * 255) 
                            : 255) * 0x1000000 + 
                r((t[0] - f[0]) * p + 
                f[0]) * 0x10000 + 
                r((t[1] - f[1]) * p + 
                f[1]) * 0x100 + 
                r((t[2] - f[2]) * p + 
                f[2])).toString(16).slice(f[3] > -1 || t[3] > -1 
                    ? 1 
                    : 3
            );
    }

    /**
     * 字符串转RGB
     * @param {*} color 
     * @returns 
     */
    this.toRGB = function(color){
        if(color.length == 4){
            color += color.substring(1);
        }
        return sbcRip(color);
    }

    /**
     * 辅助函数
     * @param {*} d 
     * @returns 
     */
    var sbcRip = function (d) {
        var i = parseInt,
            l = d.length, 
            r = Math.round, 
            RGB = new Object();
        if (l > 9) {
            d = d.split(",");
            RGB[0] = i(d[0].slice(4)), 
            RGB[1] = i(d[1]), 
            RGB[2] = i(d[2]), 
            RGB[3] = d[3] 
                ? parseFloat(d[3]) 
                : -1;
        } else {
            if (l < 6) {
                d = "#" + d[1] + d[1] + d[2] + d[2] + d[3] + d[3] + (l > 4 ? d[4] + "" + d[4] : ""); 
            }
            d = i(d.slice(1), 16), 
            RGB[0] = d >> 16 & 255, 
            RGB[1] = d >> 8 & 255, 
            RGB[2] = d & 255, 
            RGB[3] = l == 9 || l == 5 
                ? r(((d >> 24 & 255) / 255) * 10000) / 10000 
                : -1;
        }
        return RGB;
    }

    /**
     * 数组转字符串
     * @param {*} arr 
     */
    this.arr2Str = function(arr) {
        var str = "";

        for(var i in arr){
            str += "[" + i + ",";

            if(arr[i] instanceof Array){
                str += this.arr2Str(arr[i]);
            }else if(arr[i] instanceof Object){
                str += JSON.stringify(arr[i]) + ",";
            }else{
                str += arr[i] + ",";
            }

            str += "],";
        }

        if(str.lastIndexOf(",") == str.length - 1){
            str = str.substring(0, str.length - 1);
        }

        return str;
    }

    /**
     * 中文数字
     */
    const CN_NUMBER = ["零", "一","二","三","四","五","六","七","八","九"];
    
    /**
     * 中文数字单位
     */
    const CN_UNIT = ["亿","","十","百","千","万"];

    /**
     * 将数字转中文
     * @param {*} value 数字字符串
     */
    this.toChineseNumber = function(value){
        value = value.toString();

		var arr = [];

		if(value.length > 8){
			var len = Math.ceil(value.length / 8);
			var start = 0;
			var end = 0;

			for(var i = len - 1; i >= 0; i--){
				end = value.length - i * 8;
				arr.push(value.substring(start, end));
				start = end;
			}
		}else{
			arr.push(value);
		}

        var index = null;
		var ind = null;
        var mod = null;
        var cnNum = "";
		var hasZero = false;

        for(var j in arr){
			value = arr[j];
			index = value.length;

			for(var i in value){
				ind = value[i];
				mod = index % 8;

				if(ind != "0"){
					if(hasZero){
						cnNum += CN_NUMBER[0];
						hasZero = false;
					}
					
					cnNum += CN_NUMBER[ind];

					if(mod > 5 && mod < 9){
						cnNum += CN_UNIT[mod - 4];
					}else if(mod == 0){
						cnNum += CN_UNIT[4];
					}else{
						cnNum += CN_UNIT[mod];
					}
				}else{
					if(mod == 5){
						cnNum += CN_UNIT[mod];
					}else if(index > 1){
						hasZero = true;
					}else if(value.length == 1){
						cnNum += CN_NUMBER[ind];
					}
				}

				index--;
			}

            if(j < arr.length - 1){
				cnNum += CN_UNIT[0];
            }
        }

		if(cnNum.indexOf("一十") == 0){
		    cnNum = cnNum.substring(1);
		}

		if(cnNum.indexOf("亿万") > -1){
			cnNum = cnNum.replace(/[亿][万]/g, "亿");
		}

        return cnNum;
    }
}